import { login, getuserInfo } from '@/api/login'
import { getHospitalById } from '@/api/hospital'
import { getDepartmentById } from '@/api/department'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    loginInfo: null,
    longinBtnloading: false,
    userInfo: null,
    hospitalInfo: null,
    departmentInfo: null,
    chattingId: -1, // 正在聊天的id
    messageList: [], // 聊天记录
    doctorInfo: null, // 聊天对象
    formShow: 0,
    ipAddress: 'https://whczff.xyz',
    checkId: 0,
    performanceList: [
      { id: 0,
        title: '等待处理会诊数',
        count: 15,
        compareCount: '10%',
        compare: true,
        icon: 'month30'
      },
      {
        id: 1,
        title: '增在进行的会诊数',
        count: 4,
        compareCount: '10%',
        compare: false,
        icon: 'time24'
      },
      {
        id: 2,
        title: '我发起的会诊',
        count: 6,
        compareCount: '8%',
        compare: false,
        icon: 'initiator'
      },
      {
        id: 3,
        title: '被邀请的会诊',
        count: 11,
        compareCount: '10%',
        compare: true,
        icon: 'byInitiator'
      }
    ]
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, role) => {
      state.roles = role
    },
    SET_LOGINFO: (state, loginInfo) => {
      state.loginInfo = loginInfo
    },
    SET_LOGINBTNLOADING: (state, type) => {
      state.longinBtnloading = type
    },
    SET_USERINFO: (state, info) => {
      state.userInfo = info
    },
    SET_HOSPITAL: (state, info) => {
      state.hospitalInfo = info
    },
    SET_DEPARTMENT: (state, info) => {
      state.departmentInfo = info
    },
    RESET_CHATTING: (state) => {
      state.chattingId = -1
    },
    CLEAR_MESSAGE_LIST: (state) => {
      state.messageList = []
    },
    PUSH_MESSAGE: (state, message) => {
      state.messageList.push(message)
    },
    SET_CHATTING: (state, id) => {
      state.chattingId = id
    },
    SET_MESSAGE_LIST: (state, list) => {
      state.messageList = list
    },
    SET_DOCTOR_INFO: (state, info) => {
      state.doctorInfo = info
    },
    CLEAR_DOCTOR_INFO: (state) => {
      state.doctorInfo = null
    },
    SET_CHECK_ID: (state, id) => {
      state.checkId = id
    },
    SET_DASH: (state, s) => {
      state.performanceList[0].count = s.processNum
      state.performanceList[1].count = s.ingNum
      state.performanceList[2].count = s.sendNum
      state.performanceList[3].count = s.receiveNum
    }
  },

  actions: {
    setChatting({ commit }, id) {
      commit('SET_CHATTING', id)
    },
    setMessageList({ commit }, list) {
      commit('SET_MESSAGE_LIST', list)
    },
    clearMessage({ commit }) {
      commit('RESET_CHATTING')
      commit('CLEAR_MESSAGE_LIST')
      commit('CLEAR_DOCTOR_INFO')
    },
    pushMessage({ commit }, message) {
      commit('PUSH_MESSAGE', message)
    },
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password).then(response => {
          console.log(response)
          const data = response.data
          setToken(data.token)
          commit('SET_TOKEN', data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    getHospital({ commit }, id) {
      return new Promise((resolve, reject) => {
        getHospitalById({ id: id }).then(response => {
          // const data = response.data
          commit('SET_DEPARTMENT', response.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    getDepartment({ commit }, id) {
      return new Promise((resolve, reject) => {
        getDepartmentById({ id: id }).then(response => {
          // const data = response.data
          commit('SET_HOSPITAL', response.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getuserInfo(state.token).then(response => {
          const data = response.data
          commit('SET_ROLES', data.role)
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.avatar)
          commit('SET_USERINFO', data)
          const info = {
            sdkAppID: '1234',
            accountType: '1'
          }
          commit('SET_LOGINFO', info)
          response.data.loginInfo = info
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
